﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Domain.Infrastructure;
using VOL.Domain.IServices.QualityManagement;
using VOL.Entity.DomainModels;

namespace VOL.Domain.Services.QualityManagement
{
    /// <summary>
    /// 来料检验
    /// </summary>
    public class IncominginspectionService : IIncominginspectionService
    {
        private IBaseRepository<Incominginspection> _incominginspectionRepository;

        public IncominginspectionService(IBaseRepository<Incominginspection> incominginspectionRepository)
        {
            _incominginspectionRepository = incominginspectionRepository;
        }
        /// <summary>
        /// 来料检验新增
        /// </summary>
        /// <param name="incominginspection"></param>
        /// <returns></returns>
        public async Task<int> AddIncominginspection(Incominginspection incominginspection)
        {
            return await _incominginspectionRepository.Add(incominginspection);
        }
        /// <summary>
        /// 来料检验删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteIncominginspection(int id)
        {
            var incominginspection = (await _incominginspectionRepository.FindAsync(m => m.Id == id)).FirstOrDefault();
            if (incominginspection == null) return 0;
            return await _incominginspectionRepository.Delete(incominginspection);
        }
        /// <summary>
        /// 来料检验显示
        /// </summary>
        /// <returns></returns>
        public async Task<List<Incominginspection>> GetAllIncominginspection(string? incomingNumber, string? incomingName, int pageIndex = 1, int pageSize = 5)
        {
            var incominginspectionList = _incominginspectionRepository.GetAll();
            incominginspectionList = incominginspectionList.WhereIF(incomingNumber != null, m => m.IncomingName.Contains(incomingNumber));
            incominginspectionList = incominginspectionList.WhereIF(incomingName != null, m => m.IncomingName.Contains(incomingName));
            var total = incominginspectionList.Count();
            var items = incominginspectionList.OrderBy(m => m.Id).Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            return incominginspectionList.ToList();
        }
        /// <summary>
        /// 来料检验修改
        /// </summary>
        /// <param name="incominginspection"></param>
        /// <returns></returns>
        public async Task<int> UpdateIncominginspection(Incominginspection incominginspection)
        {
            return await _incominginspectionRepository.Update(incominginspection);
        }
    }
}
